package edu.upb.acs.jobsmaps.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.activity.search.SearchCompanyActivity;
import edu.upb.acs.jobsmaps.activity.search.SearchJobActivity;
import edu.upb.acs.jobsmaps.voice.WordMatcher;
import edu.upb.acs.jobsmaps.voice.WordList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by cristian on 5/18/2014.
 */
public class VoiceActivity extends Activity {
    private static final int REQUEST_CODE = 1234;
    private ListView resultList;
    Button speakButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_activity);

        speakButton = (Button) findViewById(R.id.speakButton);

		resultList = (ListView) findViewById(R.id.list);

        // Disable button if no recognition service is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() == 0) {
            speakButton.setEnabled(false);
            Toast.makeText(getApplicationContext(), "Recognizer Not Found",
                    1000).show();
        }

        speakButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startVoiceRecognitionActivity();
            }
        });
    }

    private void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Voice Recognition...");
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

			WordMatcher objectsMatcher = new WordMatcher(Arrays.asList("company", "job"));
			WordMatcher actionsMatcher = new WordMatcher(Arrays.asList("add",  "search"));
            if (0 == matches.size()) {
                System.out.println("matches number is 0");
            } else {

				for (int i = 0; i < matches.size(); i++) {
					String item = matches.get(i);
					item = item.toLowerCase();

					WordList words = new WordList(item);

					String searchAfterObject = objectsMatcher.isIn(words.getWords());
					String searchAfterAction = actionsMatcher.isIn(words.getWords());
					if (null != searchAfterAction && null != searchAfterObject) {
						if (searchAfterAction.equals("add")) {
							if (searchAfterObject.equals("job")) {
								Intent intent = new Intent(VoiceActivity.this,
										AddJobActivity.class);
								startActivity(intent);
								break;
							} else if (searchAfterObject.equals("company")) {
								Intent intent = new Intent(VoiceActivity.this,
										AddCompanyActivity.class);
								startActivity(intent);
								break;
							}

						} else if (searchAfterAction.equals("search")) {
							if (searchAfterObject.equals("job")) {
								Intent intent = new Intent(VoiceActivity.this,
										SearchJobActivity.class);
								startActivity(intent);
							} else if (searchAfterObject.equals("company")) {
								Intent intent = new Intent(VoiceActivity.this,
										SearchCompanyActivity.class);
								startActivity(intent);
							}

						}
					}
				}
			}
            resultList.setAdapter(new ArrayAdapter<>(this,
					android.R.layout.simple_list_item_1, matches));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
/*
import edu.upb.acs.jobsmaps.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import android.util.Log;



public class VoiceActivity extends Activity implements OnClickListener
{

    private TextView mText;
    private SpeechRecognizer sr;
    private static final String TAG = "MyStt3Activity";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_activity);

        Button speakButton = (Button) findViewById(R.id.speakButton);
        mText = (TextView) findViewById(R.id.textView2);
        speakButton.setOnClickListener(this);
        sr = SpeechRecognizer.createSpeechRecognizer(this);
        sr.setRecognitionListener(new listener());
    }

    class listener implements RecognitionListener
    {
        public void onReadyForSpeech(Bundle params)
        {
            Log.d(TAG, "onReadyForSpeech");
        }
        public void onBeginningOfSpeech()
        {
            Log.d(TAG, "onBeginningOfSpeech");
        }
        public void onRmsChanged(float rmsdB)
        {
            Log.d(TAG, "onRmsChanged");
        }
        public void onBufferReceived(byte[] buffer)
        {
            Log.d(TAG, "onBufferReceived");
        }
        public void onEndOfSpeech()
        {
            Log.d(TAG, "onEndofSpeech");
        }
        public void onError(int error)
        {
            Log.d(TAG,  "error " +  error);
            mText.setText("error " + error);
        }
        public void onResults(Bundle results)
        {
            String str = new String();
            Log.d(TAG, "onResults " + results);
            ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            for (int i = 0; i < data.size(); i++)
            {
                Log.d(TAG, "result " + data.get(i));
                str += data.get(i);

                String item = data.get(i);
                item = item.toLowerCase();
                Intent intent = new Intent(VoiceActivity.this,
                        AddJobActivity.class);
                    startActivity(intent);
//                if (item.matches("add company")) {
//                    Intent intent = new Intent(VoiceActivity.this,
//                            AddCompanyActivity.class);
//                    startActivity(intent);
//                    break;
//                }
//                if (item.matches("add job")) {
//                    Intent intent = new Intent(VoiceActivity.this,
//                            AddJobActivity.class);
//                    startActivity(intent);
//                    break;
//                }
//                if (item.matches("search"))
//                {
//                    Intent intent = new Intent(VoiceActivity.this,
//                            SearchJobsActivity.class);
//                    startActivity(intent);
//                    break;
//                }
            }
            mText.setText("results: "+String.valueOf(data.size()));
        }
    public void onPartialResults(Bundle partialResults)
    {
        Log.d(TAG, "onPartialResults");
    }
    public void onEvent(int eventType, Bundle params)
    {
        Log.d(TAG, "onEvent " + eventType);
    }
}
    public void onClick(View v) {
        if (v.getId() == R.id.speakButton)
        {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

            intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,5);
            sr.startListening(intent);
            Log.i("111111","11111111");
        }
    }
}
*/